package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.GoodshelvesEntity;
import io.renren.modules.sys.service.GoodshelvesService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.ShelvesgroupEntity;
import io.renren.modules.sys.service.ShelvesgroupService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-20 13:50:54
 */
@RestController
@RequestMapping("sys/shelvesgroup")
public class ShelvesgroupController {
    @Autowired
    private ShelvesgroupService shelvesgroupService;

    @Autowired
    private GoodshelvesService goodshelvesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:shelvesgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shelvesgroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:shelvesgroup:info")
    public R info(@PathVariable("id") Integer id){
        ShelvesgroupEntity shelvesgroup = shelvesgroupService.getById(id);

        return R.ok().put("shelvesgroup", shelvesgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:shelvesgroup:save")
    public R save(@RequestBody ShelvesgroupEntity shelvesgroup){
        shelvesgroupService.save(shelvesgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:shelvesgroup:update")
    public R update(@RequestBody ShelvesgroupEntity shelvesgroup){
        ValidatorUtils.validateEntity(shelvesgroup);
        shelvesgroupService.updateById(shelvesgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("sys:shelvesgroup:delete")
    public R delete(@PathVariable("id") Integer id){
        shelvesgroupService.removeById(id);

        List<GoodshelvesEntity> alllist = goodshelvesService.list(new QueryWrapper<GoodshelvesEntity>());
        if(alllist.size()>0){
            for(int i=0;i<alllist.size();i++){
                GoodshelvesEntity goodshelves = alllist.get(i);
                String group = goodshelves.getShelvesgroup();
                String temp_group = "";
                if(StringUtils.isNotBlank(group)){
                    String[] arr = group.split(",");
                    for(int j=0;j<arr.length;j++){
                        if(Integer.parseInt(arr[j])!=id){
                            temp_group += arr[j]+",";
                        }
                    }
                    if(!temp_group.equals("")){
                        temp_group = temp_group.substring(0,temp_group.length()-1);
                    }

                    goodshelves.setShelvesgroup(temp_group);
                    goodshelvesService.updateById(goodshelves);
                }

            }
        }
        return R.ok();
    }

    /**
     * 查询所有货架分组信息
     */
    @RequestMapping("/alllist")
    @RequiresPermissions("sys:shelvesgroup:alllist")
    public R alllist(){
        List<ShelvesgroupEntity> alllist = shelvesgroupService.list(new QueryWrapper<ShelvesgroupEntity>());
        return R.ok().put("alllist", alllist);
    }

}
